/**
 * 星空粒子系统可视化器
 */
class StarFieldVisualizer extends VisualizerInterface {
    constructor(canvas, ctx) {
        super(canvas, ctx);
        this.stars = [];
        this.nebula = [];
    }

    getInfo() {
        return {
            name: 'Star Field',
            description: '星空粒子系统音乐可视化',
            author: 'AudioNN',
            version: '1.0.0',
            category: 'abstract'
        };
    }

    getDefaultSettings() {
        return {
            ...super.getDefaultSettings(),
            starCount: 500,
            nebulaCount: 50,
            speed: 2,
            pulsation: true,
            colorShift: true,
            twinkle: true
        };
    }

    init(config = {}) {
        super.init(config);
        this.initializeStars();
        return this;
    }

    initializeStars() {
        this.stars = [];
        const width = this.canvas.width / window.devicePixelRatio;
        const height = this.canvas.height / window.devicePixelRatio;
        
        for (let i = 0; i < this.settings.starCount; i++) {
            this.stars.push({
                x: Math.random() * width,
                y: Math.random() * height,
                z: Math.random() * 1000,
                size: Math.random() * 3 + 1,
                speed: Math.random() * this.settings.speed + 0.5,
                twinkle: Math.random() * Math.PI * 2,
                hue: Math.random() * 360,
                intensity: 0
            });
        }
    }

    render(frequencyData, cachedData = null, currentTime = 0) {
        this.clearCanvas();
        
        const dataToUse = cachedData || frequencyData;
        if (!dataToUse) return;

        const width = this.canvas.width / window.devicePixelRatio;
        const height = this.canvas.height / window.devicePixelRatio;

        // 更新和绘制星星
        this.updateStars(dataToUse, width, height);
        this.drawStars();
        
        // 绘制星云效果
        this.drawNebula(dataToUse, width, height);
    }

    updateStars(frequencyData, width, height) {
        const avgIntensity = frequencyData.reduce((sum, val) => sum + val, 0) / frequencyData.length / 255;
        
        this.stars.forEach((star, index) => {
            const dataIndex = Math.floor((index / this.stars.length) * frequencyData.length);
            star.intensity = frequencyData[dataIndex] / 255;
            
            // 移动星星
            star.z -= star.speed * (1 + avgIntensity * 2);
            
            if (star.z <= 0) {
                star.z = 1000;
                star.x = Math.random() * width;
                star.y = Math.random() * height;
            }
            
            // 更新闪烁
            star.twinkle += 0.1;
            
            // 颜色变化
            if (this.settings.colorShift) {
                star.hue = (star.hue + star.intensity * 5) % 360;
            }
        });
    }

    drawStars() {
        const width = this.canvas.width / window.devicePixelRatio;
        const height = this.canvas.height / window.devicePixelRatio;
        
        this.stars.forEach(star => {
            const x = (star.x - width / 2) * (1000 / star.z) + width / 2;
            const y = (star.y - height / 2) * (1000 / star.z) + height / 2;
            
            if (x >= 0 && x <= width && y >= 0 && y <= height) {
                const size = star.size * (1000 / star.z) * (1 + star.intensity);
                const alpha = this.settings.twinkle ? 
                    Math.sin(star.twinkle) * 0.5 + 0.5 : 1;
                
                this.ctx.fillStyle = `hsla(${star.hue}, 80%, 70%, ${alpha * star.intensity})`;
                this.ctx.beginPath();
                this.ctx.arc(x, y, size, 0, Math.PI * 2);
                this.ctx.fill();
                
                if (star.intensity > 0.5) {
                    this.ctx.shadowColor = `hsl(${star.hue}, 80%, 70%)`;
                    this.ctx.shadowBlur = star.intensity * 20;
                    this.ctx.fill();
                    this.ctx.shadowBlur = 0;
                }
            }
        });
    }

    drawNebula(frequencyData, width, height) {
        // 简化的星云效果
        const avgIntensity = frequencyData.reduce((sum, val) => sum + val, 0) / frequencyData.length / 255;
        
        if (avgIntensity > 0.3) {
            const gradient = this.ctx.createRadialGradient(
                width / 2, height / 2, 0,
                width / 2, height / 2, Math.min(width, height) / 3
            );
            
            gradient.addColorStop(0, `rgba(138, 43, 226, ${avgIntensity * 0.3})`);
            gradient.addColorStop(1, 'rgba(138, 43, 226, 0)');
            
            this.ctx.fillStyle = gradient;
            this.ctx.fillRect(0, 0, width, height);
        }
    }

    resize(width, height) {
        this.initializeStars();
    }
}

// 注册可视化器到全局注册表
if (typeof window !== 'undefined') {
    if (!window.visualizerRegistrations) {
        window.visualizerRegistrations = [];
    }
    window.visualizerRegistrations.push({
        id: 'star-field',
        class: StarFieldVisualizer
    });
}

window.StarFieldVisualizer = StarFieldVisualizer;
